package com.mifengs.mall.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.mifengs.mall.R;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class HoneyBgView extends View {


    private int mWidth;

    private int mHeight;

    private Context mContext;

    private Xfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);

    public HoneyBgView(Context context) {
        this(context,null);
    }

    public HoneyBgView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs ,0);
    }

    public HoneyBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mWidth = w;
        this.mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        Bitmap honeyBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_honey_bg);
        Rect srcRect = new Rect(0, 0,honeyBitmap.getWidth() , honeyBitmap.getHeight());
        Rect destRect = new Rect(0, 0, mWidth, mHeight);
        Bitmap bgBitmap = creatBitmap();

        //底下的图层
        Paint resPaint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        canvas.drawBitmap(bgBitmap,0,0,resPaint);

        //最上面的图层
        canvas.drawBitmap(honeyBitmap,srcRect,destRect,paint);
        paint.setXfermode(mXfermode);
        canvas.drawBitmap(bgBitmap,0,0,paint);
     //   canvas.drawBitmap(honeyBitmap,srcRect,destRect,paint);
    }



    private Bitmap creatBitmap(){
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(ContextCompat.getColor(mContext,R.color.home_bg));
        paint.setStrokeWidth(2);
        canvas.drawRoundRect(new RectF(0,0,mWidth,2*mHeight),mHeight/2,mHeight/2,paint);
        return bitmap;
    }
}
